import { useUserInfo } from '/@/stores/userInfo';
import { judementSameArr } from '/@/utils/arrayOperation';

import { storeToRefs } from 'pinia';
import { useRoutesList } from '/@/stores/routesList';

/**
 * 循环验证
 * @param routeList 登录时返回的路由
 * @param uri 检测权限
 * */ 
const c = (routeList:any,uri:string):boolean=>{
	let right = false
	routeList.map((item)=>{
		if ( item?.children ) {
			right = c(item?.children,uri)
		} else {
			const r = item.meta?.rights
			if ( r ) {
				r.map((it:any)=>{
					if (it.uri === uri) right = true
				})
			}
		}
	})
	return right
}
/**
 * 权限验证
 * @param value 权限值
 * @returns 有权限，返回 `true`，反之则反
*/
export function checkAuth(uri : string):boolean {
	const stores = useRoutesList();
	const { routesList } = storeToRefs(stores);
	const resp = c(routesList.value,uri)
	console.log(uri,resp)
	return resp
}


/**
 * 单个权限验证
 * @param value 权限值
 * @returns 有权限，返回 `true`，反之则反
 */
export function auth(value: string): boolean {
	const stores = useUserInfo();
	return stores.userInfos.authBtnList.some((v: string) => v === value);
}

/**
 * 多个权限验证，满足一个则为 true
 * @param value 权限值
 * @returns 有权限，返回 `true`，反之则反
 */
export function auths(value: Array<string>): boolean {
	let flag = false;
	const stores = useUserInfo();
	stores.userInfos.authBtnList.map((val: string) => {
		value.map((v: string) => {
			if (val === v) flag = true;
		});
	});
	return flag;
}

/**
 * 多个权限验证，全部满足则为 true
 * @param value 权限值
 * @returns 有权限，返回 `true`，反之则反
 */
export function authAll(value: Array<string>): boolean {
	const stores = useUserInfo();
	return judementSameArr(value, stores.userInfos.authBtnList);
}
